//
//  BIDCombobox.m
//  EducationAssistant
//
//  Created by mal on 15/6/13.
//  Copyright (c) 2015年 cnsoft. All rights reserved.
//

#import "BIDCombobox.h"
#import "BIDGlobalMethods.h"
#import "UIView+FindFirstResponder.h"

/**
 *菜单未按下时的背景色
 */
#define NORMAL_COLOR [UIColor colorWithRed:73.0f/255.0f green:72.0f/255.0f blue:75.0f/255.0f alpha:1.0f]
#define PRESS_COLOR [UIColor colorWithRed:66.0f/255.0f green:65.0f/255.0f blue:67.0f/255.0f alpha:1.0f]
#define LINE_COLOR [UIColor colorWithRed:83.0f/255.0f green:82.0f/255.0f blue:84.0f/255.0f alpha:1.0f]

@interface BIDCombobox()
{
    UIView *_editView;
    UIView *_menuListView;
    UITextField *_tf;
    UIButton *_btn;
    NSArray *_menuArr;
    NSMutableArray *_btnArr;
    UIToolbar *_toolBar;
    BOOL _bHaveShowed;
    CGFloat _menuListViewHeight;
    int _selectIndex;
}

@end

@implementation BIDCombobox

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        _btnArr = [[NSMutableArray alloc] init];
        [self initView];
    }
    return self;
}

- (void)initView
{
    _selectIndex = 0;
    [self initToolBarForKeyboard];
    _editView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    _editView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _editView.layer.borderWidth = 1;
    _editView.layer.cornerRadius = 5.0f;
    _editView.clipsToBounds = YES;
    //[_editView setBackgroundColor:[UIColor blueColor]];
    _tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_editView.frame)-CGRectGetHeight(_editView.frame), CGRectGetHeight(_editView.frame))];
    _tf.textAlignment = NSTextAlignmentCenter;
    _tf.inputAccessoryView = _toolBar;
    _tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    _tf.spellCheckingType = UITextSpellCheckingTypeNo;
    _tf.autocorrectionType = UITextAutocorrectionTypeNo;
    _tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _tf.textColor = [UIColor grayColor];
    _tf.font = [UIFont systemFontOfSize:18.0f];
    _tf.enabled = NO;
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _btn.layer.borderWidth = 0.5f;
    [BIDGlobalMethods setImgForBtn:_btn imgNameForNormal:@"grayBtnBg_normal.png" imgNameForHighlight:@"grayBtnBg_highlight.png"];
    [_btn addTarget:self action:@selector(arrowBtnDownHandler) forControlEvents:UIControlEventTouchUpInside];
    [_btn setTitle:@"∨" forState:UIControlStateNormal];
    _btn.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    _btn.titleLabel.textColor = [UIColor whiteColor];
    _btn.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    _btn.frame = CGRectMake(CGRectGetMaxX(_tf.frame), 0, CGRectGetHeight(_editView.frame), CGRectGetHeight(_editView.frame));
    [_editView addSubview:_tf];
    [_editView addSubview:_btn];
    //
    [self addSubview:_editView];
}

- (void)initToolBarForKeyboard
{
    //toolBar
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGSize toolBarSize = CGSizeMake(screenSize.width, 30);
    CGRect toolBarFrame = CGRectMake(0, 0, toolBarSize.width, toolBarSize.height);
    _toolBar = [[UIToolbar alloc] initWithFrame:toolBarFrame];
    _toolBar.tintColor = [UIColor whiteColor];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"关闭键盘" style:UIBarButtonItemStyleBordered target:self action:@selector(closeKeyboardHandler)];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [_toolBar setItems:[[NSArray alloc] initWithObjects:item2, item1, nil]];
    _toolBar.barStyle = UIBarStyleBlack;
    _toolBar.translucent = YES;
}

- (void)setMenuListView:(NSArray *)menuArr
{
    CGRect listViewFrame = CGRectMake(0, CGRectGetMaxY(_editView.frame), CGRectGetWidth(self.frame), 0);
    _menuListView = [[UIView alloc] initWithFrame:listViewFrame];
    [_menuListView setBackgroundColor:[UIColor lightGrayColor]];
    _menuListView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _menuListView.layer.borderWidth = 0.5f;
    int topSpacing = 0;
    int bottomSpacing = 0;
    int interval = 1;
    UIButton *btn;
    _menuArr = [[NSArray alloc] initWithArray:menuArr];
    CGSize btnSize = CGSizeMake(CGRectGetWidth(self.frame), 35);
    for(int i=0; i<_menuArr.count; i++)
    {
        CGRect btnFrame;
        CGRect lineFrame;
        if(i==0)
        {
            btnFrame = CGRectMake(0, topSpacing, btnSize.width, btnSize.height);
        }
        else
        {
            btn = (UIButton*)[_btnArr objectAtIndex:i-1];
            btnFrame = CGRectMake(0, btn.frame.origin.y+btn.frame.size.height+interval+1, btnSize.width, btnSize.height);
        }
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = btnFrame;
        btn.tag = i;
        [self setButtonAttribute:btn text:[_menuArr objectAtIndex:i]];
        [_menuListView addSubview:btn];
        [_btnArr addObject:btn];
        //
        if(i<_menuArr.count-1)
        {
            lineFrame = CGRectMake(0, CGRectGetMaxY(btn.frame)+1, CGRectGetWidth(self.frame), 1);
            UILabel *label = [[UILabel alloc] initWithFrame:lineFrame];
            [label setBackgroundColor:LINE_COLOR];
            [_menuListView addSubview:label];
        }
    }
    listViewFrame.size.height = btnSize.height * _btnArr.count + 2*interval * (_btnArr.count-1) + topSpacing + bottomSpacing;
    _menuListViewHeight = listViewFrame.size.height;
    _menuListView.frame = listViewFrame;
}
/**
 *显示菜单
 */
- (void)arrowBtnDownHandler
{
    if(!_bHaveShowed)
    {
        _bHaveShowed = YES;
        [self showMenu:YES];
    }
    else
    {
        _bHaveShowed = NO;
        [self showMenu:NO];
    }
}
- (void)showMenu:(BOOL)bShow
{
    __block CGRect frame = self.frame;
    if(bShow)
    {
        [self addSubview:_menuListView];
        [UIView animateWithDuration:0.5f animations:^{
            //_menuListView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
        } completion:^(BOOL finished){
            if(finished)
            {
                frame.size.height += _menuListViewHeight;
                self.frame = frame;
            }
        }];
    }
    else
    {
        [UIView animateWithDuration:0.5f animations:^{
            //_menuListView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 0);
        } completion:^(BOOL finished){
            if(finished)
            {
                [_menuListView removeFromSuperview];
                frame.size.height -= _menuListViewHeight;
                self.frame = frame;
            }
        }];
    }
}

- (void)setButtonAttribute:(UIButton *)btn text:(NSString *)text
{
    btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [btn setTitle:text forState:UIControlStateNormal];
    [btn setBackgroundColor:NORMAL_COLOR];
    [btn addTarget:self action:@selector(btnDownHandler:) forControlEvents:UIControlEventTouchDown];
    [btn addTarget:self action:@selector(btnUpInsideHandler:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(btnUpOutsideHandler:) forControlEvents:UIControlEventTouchCancel];
}

- (void)closeKeyboardHandler
{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *firstResponderView = [keyWindow findFirstResponder];
    [firstResponderView resignFirstResponder];
}

- (void)btnDownHandler:(UIButton *)btn
{
    [btn setBackgroundColor:PRESS_COLOR];
}

- (void)btnUpInsideHandler:(UIButton*)btn
{
    _bHaveShowed = NO;
    [btn setBackgroundColor:NORMAL_COLOR];
    //[delegate selectMenuAtIndex:btn.tag];
    _tf.text = _menuArr[btn.tag];
    _selectIndex = btn.tag;
    [_menuListView removeFromSuperview];
}

- (void)btnUpOutsideHandler:(UIButton *)btn
{
    [btn setBackgroundColor:NORMAL_COLOR];
}

- (NSString*)getContent
{
    return _tf.text;
}

- (int)getIndex
{
    return _selectIndex;
}

@end
